package com.sda.huoshui.fragment

import android.annotation.SuppressLint
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import com.youth.banner.Banner
import com.sda.huoshui.view.NoScrollRecyclerView
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.sda.huoshui.R
import com.google.gson.TypeAdapter
import com.youth.banner.loader.ImageLoader
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.sda.huoshui.bean.ShouYeBean
import com.sda.huoshui.util.CommonTools
import com.wzd.taijiapp.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_shou_ye.*
import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.ArrayList

class ShouYeFragment : BaseFragment() {

    private val mLabelList: ArrayList<ShouYeBean.DataBean.LableBean> = ArrayList()
    private val mTypeList: ArrayList<ShouYeBean.DataBean.MediaClassBean> = ArrayList()
    private val mKeChengTopList: ArrayList<ShouYeBean.DataBean.TopBean> = ArrayList()
    private val mKeChengBottomList: ArrayList<ShouYeBean.DataBean.BottomBean> =ArrayList()
    private val mBannerList: ArrayList<ShouYeBean.DataBean.RotationBean> = ArrayList()
    private val mBannerList2: ArrayList<ShouYeBean.DataBean.RotationBean> = ArrayList()
    private val mBannerList3: ArrayList<ShouYeBean.DataBean.RotationBean> = ArrayList()

    override fun initView(): Int {
       return R.layout.fragment_shou_ye
    }

    override fun initData() {
        recyclerViewTypeOne.adapter = LabelAdapter(mLabelList)
        recyclerViewTypeTwo.adapter = TypeAdapter(mTypeList)
        recyclerViewKeChengTop.adapter = MyTopAdapter(mKeChengTopList)
        recyclerViewKeChengOther.adapter = MyBottomAdapter(mKeChengBottomList)

        getCateData()
    }

    private fun initBannerOne() {
        val images = ArrayList<String?>()
        for (i in mBannerList.indices) {
            images.add(mBannerList[i].cover_image)
        }
        banner.visibility = View.VISIBLE
        banner!!.setDelayTime(3000)
        banner!!.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context, path: Any, imageView: ImageView) {
                Glide.with(context).load(path.toString()).into(imageView)
            }
        }).setImages(images).start()
    }

    private fun initBannerTwo() {
        val images = ArrayList<String?>()
        for (i in mBannerList2.indices) {
            images.add(mBannerList2[i].cover_image)
        }
        banner2.visibility = View.VISIBLE
        banner2!!.setDelayTime(3000)
        banner2!!.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context, path: Any, imageView: ImageView) {
                Glide.with(context).load(path.toString()).into(imageView)
            }
        }).setImages(images).start()
    }

    private fun initBannerThree() {
        val images = ArrayList<String?>()
        for (i in mBannerList3.indices) {
            images.add(mBannerList3[i].cover_image)
        }
        banner3.visibility = View.VISIBLE
        banner3!!.setDelayTime(3000)
        banner3!!.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context, path: Any, imageView: ImageView) {
                Glide.with(context).load(path.toString()).into(imageView)
            }
        }).setImages(images).start()
    }

    inner class LabelAdapter(data: List<ShouYeBean.DataBean.LableBean>?) : BaseQuickAdapter<ShouYeBean.DataBean.LableBean, BaseViewHolder>(R.layout.item_label, data) {
        override fun convert(helper: BaseViewHolder, item: ShouYeBean.DataBean.LableBean) {
            helper.setText(R.id.tvName, item.label)
        }
    }

    inner class TypeAdapter(data: List<ShouYeBean.DataBean.MediaClassBean>?) : BaseQuickAdapter<ShouYeBean.DataBean.MediaClassBean, BaseViewHolder>(R.layout.item_type, data) {
        override fun convert(helper: BaseViewHolder, item: ShouYeBean.DataBean.MediaClassBean) {
            helper.setText(R.id.tvName, item.name)
        }
    }

    //列表的适配器
    inner class MyTopAdapter(data: List<ShouYeBean.DataBean.TopBean>) :
        BaseQuickAdapter<ShouYeBean.DataBean.TopBean, BaseViewHolder>(
            R.layout.item_kc_top,
            data
        ) {

        override fun convert(helper: BaseViewHolder, item: ShouYeBean.DataBean.TopBean) {
            val recyclerViewKeCheng = helper.getView<NoScrollRecyclerView>(R.id.recyclerViewKeCheng)
            helper.setText(R.id.tvTitle,item.tile)
            recyclerViewKeCheng.adapter = KeChengAdapter(item.content)
        }
    }

    //列表的适配器
    inner class MyBottomAdapter(data: List<ShouYeBean.DataBean.BottomBean>) :
        BaseQuickAdapter<ShouYeBean.DataBean.BottomBean, BaseViewHolder>(
            R.layout.item_kc_top,
            data
        ) {

        override fun convert(helper: BaseViewHolder, item: ShouYeBean.DataBean.BottomBean) {
            val recyclerViewKeCheng = helper.getView<NoScrollRecyclerView>(R.id.recyclerViewKeCheng)
            helper.setText(R.id.tvTitle,item.tile)
            recyclerViewKeCheng.adapter = KeChengAdapter(item.content)
        }
    }

    //列表的适配器
    inner class KeChengAdapter(data: List<ShouYeBean.DataBean.ContentBean>) :
        BaseQuickAdapter<ShouYeBean.DataBean.ContentBean, BaseViewHolder>(
            R.layout.item_kecheng,
            data
        ) {

        override fun convert(helper: BaseViewHolder, item: ShouYeBean.DataBean.ContentBean) {
            val ivPic = helper.getView<ImageView>(R.id.ivPic)
            CommonTools.showGlideImage(requireContext(),item.image?:"",ivPic)
            helper.setText(R.id.tvTitle,item.name)
                .setText(R.id.tvTime,miaoToTime(item.audio_num?:"0"))
                .setText(R.id.tvRemark,item.describe)
        }
    }

    fun miaoToTime(miao:String):String {

        val milliseconds: Long = miao.toLong()*1000

        val minutes = milliseconds / 1000 / 60
        val seconds = milliseconds / 1000 % 60

        return "$minutes:$seconds"
    }

    private fun getCateData() {
        val map = HashMap<String, Any>()
        map["id"] = 33
        mCompositeSubscription!!.add(
            retrofitService!!.getCateData(map).subscribeOn(Schedulers.io()).observeOn(
                AndroidSchedulers.mainThread()
            ).subscribe(
                object : Observer<ResponseBody> {
                    override fun onCompleted() {
                        cancelProgressDialog()
                    }

                    override fun onError(e: Throwable) {
                        e.printStackTrace()
                        cancelProgressDialog()
                    }

                    override fun onNext(data: ResponseBody) {
                        val str = data.string()
                        CommonTools.parsingReturnData(str,
                            object : CommonTools.OnParsingReturnListener {
                                @SuppressLint("NotifyDataSetChanged")
                                override fun onParsingSuccess() {
                                    val bean = Gson().fromJson(str, ShouYeBean::class.java).data
                                    bean?.let {
                                        mLabelList.clear()
                                        mLabelList.addAll(it.lable)
                                        recyclerViewTypeOne.adapter?.notifyDataSetChanged()

                                        mTypeList.clear()
                                        mTypeList.addAll(it.media_class)
                                        recyclerViewTypeTwo.adapter?.notifyDataSetChanged()

                                        mKeChengTopList.clear()
                                        it.top?.let { it1 -> mKeChengTopList.add(it1) }
                                        recyclerViewKeChengTop.adapter?.notifyDataSetChanged()

                                        mKeChengBottomList.clear()
                                        mKeChengBottomList.addAll(it.bottom)
                                        recyclerViewKeChengOther.adapter?.notifyDataSetChanged()

                                        mBannerList.clear()
                                        mBannerList.addAll(it.rotation_1)
                                        if (mBannerList.size>0){
                                            initBannerOne()
                                        }

                                        mBannerList2.clear()
                                        mBannerList2.addAll(it.rotation_2)
                                        if (mBannerList2.size>0){
                                            initBannerTwo()
                                        }

                                        mBannerList3.clear()
                                        mBannerList3.addAll(it.rotation_3)
                                        if (mBannerList3.size>0){
                                            initBannerThree()
                                        }
                                    }
                                }
                            })
                    }
                })
        )
    }
}